package com.lv.dhart.ui.activity;


import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lv.dhart.R;
import com.lv.dhart.beans.as4kdots.CostChangeBeans;
import com.lv.dhart.beans.cost.AddResultBean;
import com.lv.dhart.callback.cost.ICostAddCallback;
import com.lv.dhart.custom.fragment.baseframework.BaseActivity;
import com.lv.dhart.custom.fragment.baseframework.interfaces.Layout;
import com.lv.dhart.custom.fragment.baseframework.util.JumpParameter;
import com.lv.dhart.presenter.CostPresenter;
import com.lv.dhart.widget.text_edit.CustomTextEdit;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;

@Layout(R.layout.activity_cost_rl)
public class CostActivity extends BaseActivity implements ICostAddCallback {

    private CostPresenter costPresenter;
    private Button btnComputerNewCostCount;
    /**
     * 估算主力成本
     */
    private Button btnComputerMainCost;

    private EditText etOriginPrice;
    private EditText etOriginCount;
    private EditText etCurrentPrice;
    private EditText etCountChange;
    private EditText etAmountChange;
    private EditText tvNewPriceByAdd;
    private EditText tvNewCountByAdd;
    private EditText tvNewPriceByMinus;
    private EditText tvNewCountByMinus;

    private CustomTextEdit etOriginPriceCTE;
    private CustomTextEdit etOriginCountCTE;
    private CustomTextEdit etCurrentPriceCTE;
    private CustomTextEdit etCountChangeCTE;
    private CustomTextEdit etAmountChangeCTE;
    private CustomTextEdit tvNewPriceByAddCTE;
    private CustomTextEdit tvNewPriceByMinusCTE;
    private CustomTextEdit tvNewCountByAddCTE;
    private CustomTextEdit tvNewCountByMinusCTE;

    CostChangeBeans.DataDTO dataDTO;

    @Override
    protected void initPresenter() {
        costPresenter = new CostPresenter();
    }

    @Override
    public void initViews() {

        etOriginPriceCTE = findViewById(R.id.et_origin_price);
        etOriginCountCTE = findViewById(R.id.et_origin_count);
        etCurrentPriceCTE = findViewById(R.id.et_current_price);
        etCountChangeCTE = findViewById(R.id.et_count_change);
        etAmountChangeCTE = findViewById(R.id.et_amount_change);
        tvNewPriceByAddCTE = findViewById(R.id.tv_new_price_by_add);
        tvNewPriceByMinusCTE = findViewById(R.id.tv_new_price_by_minus);
        tvNewCountByAddCTE = findViewById(R.id.tv_new_count_by_add);
        tvNewCountByMinusCTE = findViewById(R.id.tv_new_count_by_minus);
        btnComputerNewCostCount = findViewById(R.id.btn_computer_new_cost_count);
        btnComputerMainCost = findViewById(R.id.btn_computer_main_cost);
        etOriginPrice = etOriginPriceCTE.getEditText();
        etOriginCount = etOriginCountCTE.getEditText();
        etCurrentPrice =  etCurrentPriceCTE.getEditText();
        etCountChange = etCountChangeCTE.getEditText();
        etAmountChange = etAmountChangeCTE.getEditText();
        tvNewPriceByAdd = tvNewPriceByAddCTE.getEditText();
        tvNewCountByAdd = tvNewCountByAddCTE.getEditText();
        tvNewPriceByMinus = tvNewPriceByMinusCTE.getEditText();
        tvNewCountByMinus = tvNewCountByMinusCTE.getEditText();
    }

    @Override
    public void initDatas(JumpParameter parameter) {
        dataDTO = parameter.getMapObjKey("cost");
        etOriginPrice.setText(dataDTO.getOriginCost());
        etOriginCount.setText(dataDTO.getOriginCount());
        etCountChange.setText(dataDTO.getAddCount());
        etCurrentPrice.setText(dataDTO.getCurrentPrice());
    }

    @Override
    public void setEvents() {

        btnComputerNewCostCount.setOnClickListener(view -> {
            if (TextUtils.isEmpty(etOriginPrice.getText().toString())
                    || TextUtils.isEmpty(etOriginCount.getText().toString())
                    || TextUtils.isEmpty(etCurrentPrice.getText().toString())
                    || TextUtils.isEmpty(etCountChange.getText().toString())) {
                Toast.makeText(me, "数量或价格数据不完整！", Toast.LENGTH_SHORT).show();
                return;
            }

            //原始股价
            BigDecimal originPrice = new BigDecimal(etOriginPrice.getText().toString());
            //原始数量
            BigDecimal originCount = new BigDecimal(etOriginCount.getText().toString());
//                System.out.println("all: " + originPrice.multiply(originCount));
            //当前股价
            BigDecimal nowPrice = new BigDecimal(etCurrentPrice.getText().toString());
            //当前预加减的数量
            BigDecimal addCount = new BigDecimal(etCountChange.getText().toString());
            //加仓后新的价格
            BigDecimal newPricePlus = originPrice.multiply(originCount)
                    .add(nowPrice.multiply(addCount))
                    .divide(originCount.add(addCount), 2, RoundingMode.HALF_UP);
            // 增减金额
            etAmountChange.setText(nowPrice.multiply(addCount).toPlainString());
            tvNewPriceByAdd.setText(newPricePlus.toPlainString());
            tvNewCountByAdd.setText(addCount.add(originCount).toPlainString());
            try {
                BigDecimal newPriceMinus = originPrice.multiply(originCount)
                        .subtract(nowPrice.multiply(addCount))
                        .divide(originCount.subtract(addCount), 2, RoundingMode.HALF_UP);
                tvNewPriceByMinus.setText(newPriceMinus.toPlainString());
                tvNewCountByMinus.setText(originCount.subtract(addCount).toPlainString());
            } catch (ArithmeticException e) {
                Toast.makeText(me, "估计你要清仓了！", Toast.LENGTH_SHORT).show();
            }
//            dataDTO.setHighestPrice(etHighestPrice.getText().toString());
//            dataDTO.setLowestPrice(etLowestPrice.getText().toString());
//            dataDTO.setMidPrice(etMidPrice.getText().toString());
            dataDTO.setAddAmount(etAmountChange.getText().toString());
            dataDTO.setAddCount(etCountChange.getText().toString());
            dataDTO.setCurrentPrice(etCurrentPrice.getText().toString());
//            dataDTO.setFallPricePercentage(tvPriceFallRange.getText().toString());
            dataDTO.setNewCountMinus(tvNewCountByMinus.getText().toString());
            dataDTO.setOriginCost(etOriginPrice.getText().toString());
            dataDTO.setOriginCount(etOriginCount.getText().toString());
            dataDTO.setNewPricePlus(tvNewPriceByAdd.getText().toString());
            dataDTO.setNewPriceMinus(tvNewPriceByMinus.getText().toString());
//            dataDTO.setUpPricePercentage(tvPriceUpRange.getText().toString());
            HashMap<String, String> params = new HashMap<>();
            params.put("id", String.valueOf(dataDTO.getId()));
            params.put("stock_code", dataDTO.getStockCode());
            params.put("stock_name", dataDTO.getStockName());
            params.put("current_price", dataDTO.getCurrentPrice());
            params.put("origin_cost", dataDTO.getOriginCost());
            params.put("origin_count", dataDTO.getOriginCount());
            params.put("add_amount", dataDTO.getAddAmount());
            params.put("add_count", dataDTO.getAddCount());
            params.put("highest_price", dataDTO.getHighestPrice());
            params.put("lowest_price", dataDTO.getLowestPrice());
            params.put("mid_price", dataDTO.getMidPrice());
            params.put("up_price_percentage", dataDTO.getUpPricePercentage());
            params.put("fall_price_percentage", dataDTO.getFallPricePercentage());
            params.put("new_price_plus", dataDTO.getNewPricePlus());
            params.put("new_price_minus", dataDTO.getNewPriceMinus());
            params.put("new_count_plus", dataDTO.getNewCountPlus());
            params.put("new_count_minus", dataDTO.getNewCountMinus());
            costPresenter.addCostStock(params, CostActivity.this);
            //计算主力成本

        });
    }


    @Override
    public void onCostAddSuccess(AddResultBean costChangeBeans) {

    }

    @Override
    public void onCostAddFailure(Throwable throwable) {

    }
}